﻿namespace Heroius.Extension.CSV
{
    /// <summary>
    /// 提供用于操作CSV文件的静态方法
    /// </summary>
    public static class CsvHelper
    {
        /// <summary>
        /// 从CSV文件的一行文本分析获取由逗号分隔的数据
        /// </summary>
        /// <param name="DataLine"></param>
        /// <returns></returns>
        public static List<string> ReadLine(string DataLine)
        {
            List<string> TempRow = new List<string>();
            //定义：用于构建单元格数据的临时字符串；进入以引号开启的数据；定义是否启动了转义
            string TempItem = ""; bool Inside = false; bool Alter = false;
            //考察每一个字符
            //foreach(char CharNow in DataLine)
            for (int i = 0; i < DataLine.Length; i++)
            {
                char CharNow = DataLine[i];
                if (Inside)
                {
                    if (Alter)//上一个为引号，且那引号未加入
                    {
                        Alter = false;
                        //连续两个引号，是为转义，加入该引号
                        if (CharNow == '\"') { TempItem += '\"'; continue; }
                        else
                        {
                            Inside = false; //若上一个为引号但这个不是，则说明引号包容结束
                            if (CharNow == ',')//若为逗号，则说明该单元格结束
                            {
                                //在修改临时字符串之前，将其添加到临时行
                                TempRow.Add(TempItem);
                                TempItem = "";
                                continue;
                            }
                            else { TempItem += CharNow; continue; }
                        }
                    }//end of Alter
                    else//在引号包容内部添加字符
                    {
                        if (CharNow == '\"') { Alter = true; continue; }
                        else { TempItem += CharNow; continue; }
                    }
                }//end of Inside
                else
                {
                    switch (CharNow)
                    {
                        case '\"': Inside = true; continue;
                        case ',': TempRow.Add(TempItem); TempItem = ""; continue;
                        default: TempItem += CharNow; continue;
                    }
                }
            }//end of for loop
            //添加最后一个字符串
            TempRow.Add(TempItem);
            return TempRow;
        }

        /// <summary>
        /// 将提供的数据书写成CSV格式的字符串
        /// </summary>
        /// <param name="SpecifiedRow"></param>
        /// <returns></returns>
        public static string WriteLine(IEnumerable<string> SpecifiedRow)
        {
            string TempLine = "";
            for (int i = 0; i < SpecifiedRow.Count(); i++)
            {
                string Item = SpecifiedRow.ElementAt(i) ?? "";
                Item = Item.Replace("\"", "\"\"");
                if (Item.Contains("\"") || Item.Contains(","))
                    Item = "\"" + Item + "\"";
                TempLine += Item;
                if (i < SpecifiedRow.Count() - 1)
                    TempLine += ",";
            }
            return TempLine;
        }
    }
}
